<?
require_once('functions.php');

function GetAddressTypes()
{
        $conn = GetConnection();
        return $conn->query('SELECT * FROM 2012WP_AddressType');
}
function BlankAddressType()
{
        return array('Description'=>null,'id'=>null);
}
function GetAddressType($id)
{
        $conn = GetConnection();
        $sql = "SELECT * FROM 2012WP_AddressType WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}
function SaveAddressType(&$row)
{
        $conn = GetConnection();
        $row2 = EscapeRow($row, $conn);
        if(empty($row['id']))
        {
                $sql =  "Insert 2012WP_AddressType (Description) "
                        .       " Values ('$row2[Description]') ";
        }else{
                $sql =  "UPDATE 2012WP_AddressType "
                        .       "Set Description='$row2[Description]' "
                        .       "WHERE id=$row2[id] ";
        }
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        if(empty($row['id']) && empty($error))
        {
                $row['id'] = $conn->insert_id;
        }
        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}
function DeleteAddressType($id)
{
        $conn = GetConnection();
        $sql =  "DELETE FROM 2012WP_AddressType WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}
function ValidateAddressType($row)
{
        $errors = array();
        if(empty($row['Description'])) $errors['Description'] = 'Description is required';
       
        return count($errors) == 0 ? true : $errors;
}
